#include "Queue.h"

void SListPrint(QNode* phead)
{
	QNode* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

void QueueInit(Queue* q)
{
	assert(q);
	q->front = q->rear = NULL;
	q->size = 0;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("newnode malloc fault");
		return;
	}
	newnode->data = data;
	newnode->next = NULL;

	if (q->rear == NULL)
	{
		q->front = q->rear = newnode;
	}
	else
	{
		q->rear->next = newnode;
		q->rear = newnode;
	}
	q->size++;
}

void QueuePop(Queue* q)
{
	assert(q);
	assert(q->size > 0);

	QNode* del = q->front;
	q->front = q->front->next;

	free(del);
	del = NULL;

	q->size--;
	if (q->size == 0)
		q->rear = NULL;
}

QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(q->size > 0);

	return q->front->data;
}

QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(q->size > 0);

	return q->rear->data;
}

int QueueSize(Queue* q)
{
	assert(q);

	return q->size;
}

int QueueEmpty(Queue* q)
{
	assert(q);

	return !(q->size);
}

void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* cur = q->front;

	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	cur = NULL;
	q->front = q->rear = NULL;
	q->size = 0;
}